package com.zbkj.crmeb.store.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.crmeb.chant.model.ChannelMerchantAccount;
import com.zbkj.crmeb.chant.service.IChannelMerchantAccountService;
import com.zbkj.crmeb.enums.AccountFlowTypeEnum;
import com.zbkj.crmeb.store.dao.AccountFlowDao;
import com.zbkj.crmeb.store.model.AccountFlow;
import com.zbkj.crmeb.store.service.AccountFlowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@Slf4j
@Service
public class AccountFlowServiceImpl extends ServiceImpl<AccountFlowDao, AccountFlow> implements AccountFlowService {
    
    @Resource
    private AccountFlowDao accountFlowDao;
    @Resource
    private IChannelMerchantAccountService channelMerchantAccountService;
    
    @Override
    public void saveAccountFlow(Integer merId, Integer otId, Integer operateId, String operateIp, AccountFlowTypeEnum tradeType, 
        BigDecimal account, BigDecimal withdrawAccount, BigDecimal freezeAccount, String remark) {
    	// 流水操作
        ChannelMerchantAccount channelMerchantAccount = channelMerchantAccountService.getOne(Wrappers.<ChannelMerchantAccount>lambdaQuery()
                .eq(ChannelMerchantAccount :: getMerId, merId));

        AccountFlow accountFlow = new AccountFlow();
        accountFlow.setMerId(merId);
        accountFlow.setOtId(otId);
        accountFlow.setOperateId(operateId);
        accountFlow.setOperateIp(operateIp);
        accountFlow.setTradeType(tradeType);
        accountFlow.setAccount(account);
        accountFlow.setWithdrawAccount(withdrawAccount);
        accountFlow.setFreezeAccount(freezeAccount);
        accountFlow.setRemark(remark);
        accountFlow.setNowAccount(channelMerchantAccount.getAccount());
        accountFlow.setNowWithdrawAccount(channelMerchantAccount.getWithdrawAccount());
        accountFlow.setNowFreezeAccount(channelMerchantAccount.getFreezeAccount());
        accountFlow.setCreateTime(new Date());
        accountFlow.setUpdateTime(new Date());

        accountFlowDao.insert(accountFlow);
        log.info("新增资金流水, 商户: {}, JSON: {}", merId, JSONObject.toJSONString(accountFlow));
    }
    
}
